//
//  GLCountry.swift
//  FanweApp
//
//  Created by zhuxietong on 2019/5/14.
//  Copyright © 2019 xfg. All rights reserved.
//

import Foundation
import Eelay
public var InitObject = NSMutableDictionary()

public class GLCountry: NSObject {
    
    public enum Notice:String,NotifyType {
        
        case change = "change"
        
        public var name: Notification.Name{
            switch self {
            case .change:
                return "GLCountry.Notice".notice_name
            }
//            return "JoUser.\(Notice.self)\(self.rawValue)".notice_name
        }
    }
    
    public static var code:String{
        get{
            if let code = UserDefaults.standard.value(forKey: "GLCountry/code") as? String{
                return code
            }
            return "86"
        }
        set{
            UserDefaults.standard.set(newValue, forKey: "GLCountry/code")
            let c = currentCountry
//            print("+++III|\(c)")
            MoLocalizable.lproj = c["lproj",""]
            __post(notify: Notice.change)
        }
    }
    
    public static var countrys:[[String:Any]] = [
        ["id":"0","code":"86","name":"中国".loc,"lproj":"zh-Hans","lunguage":"简体中文".loc],
        ["id":"1","code":"66","name":"泰国".loc,"lproj":"th","lunguage":"泰语".loc],
        ["id":"2","code":"122","name":"韩国".loc,"lproj":"co","lunguage":"韩语".loc],
    ]
//    static var countrys:[[String:Any]]{
//        get{
//
//
//
//            var cs = DataNode.path(node: "country.support", InitObject, value: NSMutableArray())
//
//            if cs.count < 1{
//                cs = [
//                    ["code":"01","name":"American","money":"$"],
//                    ["code":"66","name":"Thai","money":"$"],
//                ]
//            }
//
//            var result:[[String:Any]] = []
//            let ct = DataNode.path(node: "country.current", InitObject, value: NSMutableDictionary())
//
//            var current_in_support = false
//            cs.list { (obj:NSMutableDictionary, i) in
//                result.append(obj.swift_dictionary)
//                if (obj["code","XX"] == ct["code","YY"])
//                {
//                    current_in_support = true
//                }
//            }
//            if !current_in_support{
//                result.insert(ct.swift_dictionary, at: 0)
//            }
//            return result
//        }
//    }
//
//    static var currentAreaCode:String{
//        get{
//            return currentCountry["code","66"]
//        }
//    }

    
//    static var currentCountry:NSMutableDictionary = ["code":"86","name":"中国"].mutable_dictionary
    public static var currentCountry:NSMutableDictionary{
        get{
            var result = NSMutableDictionary()
            let code = GLCountry.code
            for one in countrys {
                let obj = one.mutable_dictionary
                if obj["code",""] == code{
                     result = obj
                }
            }
            result.startSync(scheme: .init(keys: ["mark",""], primaryKey: "id", table: "GLCountry"))
            return result
        }
    }

}
